// const { addUser } = require('../service/user.service')
const fs = require('fs')
const path = require('path')

const { unSupportedFileType, fileUploadError } = require('../constant/err.type')

class BannersController {

	async upload(ctx) {
		const { file } = ctx.request.files
		// console.log(file)
		const fileTypes = ['image/jpeg', 'image/png', 'image/jpg']
		if (file) {
			if (!fileTypes.includes(file.type)) {
				return ctx.app.emit('error', unSupportedFileType, ctx)
			}
			ctx.body = {
				code: 0,
				message: '商品图片上传成功',
				result: {
					goods_img: path.basename(file.path),
				},
			}
		} else {
			return ctx.app.emit('error', fileUploadError, ctx)
		}
	}

	async getBanners(ctx) {
		const url = 'http://localhost:5005/'
		var imageList = [];
		const p = path.join(__dirname,'../upload')
		const files = fs.readdirSync(p);
		files.forEach(item => {
			imageList.push(url + item)
		});
		ctx.body = {
			code: 0,
			message: '这是banner图',
			result: {
				imageList
			},
		}
	}
}
module.exports = new BannersController()